package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public Result list(UserDto userDto){
        log.info("查询所有用户" + userDto);
        PageResult pageResult =userService.list(userDto);
        return Result.success(pageResult);
    }

    @Log
    @PostMapping
    public Result add(@RequestBody User user){
        log.info("添加用户" + user);
        userService.add(user);
        return Result.success();
    }

    @Log
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("删除用户" + ids);
        userService.deleteByIds(ids);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("查询id:{}",id);
        User user =userService.findById(id);
        return Result.success(user);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody User user){
        log.info("更新用户" + user);
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/list")
    public Result allUsers(){
        log.info("查询所有用户");
        List<User> users =userService.allUsers();
        return Result.success(users);
    }

    @GetMapping("/role/{roleLabel}")
    public Result getRoleLabel(@PathVariable String roleLabel){
        log.info("查询角色" + roleLabel);
        List<User> user =userService.getRoleLabel(roleLabel);
        return Result.success(user);
    }

    @GetMapping("/dept/{deptId}")
    public Result getDeptId(@PathVariable Integer deptId){
        log.info("查询部门" + deptId);
        List<User> user =userService.getDeptId(deptId);
        return Result.success(user);
    }
}
